<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <meta name="viewport"
        content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0" />
    <meta name="format-detection" content="telephone=no,email=no,date=no,address=no">
    <link rel="stylesheet" type="text/css" href="../css/aui.2.0.css" />
    <style>
        .centered-placeholder {
            width: 80%;
            font-size: 16px;
            text-align: center;
        }
    </style>
</head>

<body>
    <header class="aui-bar aui-bar-nav main-header">
        <a class="aui-pull-left aui-btn" tapmode onclick="closeWin();">
            <span class="aui-iconfont aui-icon-left"></span>
        </a>
        <div class="aui-title" id="title">
            涂装上线绑定
        </div>
    </header>
    <div class="aui-bar aui-bar-nav sub-header"></div>
    <div class="aui-content aui-margin-b-15" style="margin-top:50px;">
        <div class="white_background">
            <ul class="aui-list aui-form-list">

                <li class="aui-list-item">
                    <div class="aui-list-item-inner">
                        <div class="aui-list-item-label">滑橇号</div>
                        <input type="text" class="aui-input" placeholder="请输入滑橇号" readonly
                            style="width: 80%;font-size: 16px;" id="skiddingCode" />
                        <div class="aui-btn aui-btn-info" style="width:25% ;margin-right:2%;" onclick="finishOther()">
                            下一项</div>
                    </div>
                </li>
                <li class="aui-list-item" style="background-color: #D7D7D7;">
                    <div class="aui-list-item-inner">
                        <!-- <input type="text" class="aui-input centered-placeholder" placeholder="提示消息" readonly
                    style="color: black; font-size: 12px;" id="message" /> -->
                        <div style="color: black; font-size: 14px;" id="message">

                        </div>
                    </div>
                </li>
                <li class="aui-list-item">
                    <div class="aui-list-item-inner">
                        <div class="aui-list-item-label">容器条码</div>
                        <input type="text" class="aui-input" placeholder="请扫描容器条码" style="width: 80%;font-size: 16px;"
                            id="containerTm" onclick="getIdSelector();" />
                        <i class="aui-input-addon aui-iconfont aui-icon-scan "
                            style="padding-right:10px;font-size: 1.6rem" onclick="doScan();"></i>
                    </div>
                </li>
            </ul>
        </div>
        <div id="table1">

        </div>

    </div>
    <footer>
        <div class="aui-btn aui-btn-info aui-btn-block"
            style="margin:0 5%;width: 90%;background-color:red !important;color:#f5f5f5;" onclick="chooseUnbind()">选中解绑
        </div>
    </footer>

</body>
<script type="text/javascript" src="../script/api.js"></script>
<script type="text/javascript" src="../script/doT.min.js"></script>
<script type="text/javascript" src="../script/public.js"></script>
<script type="text/javascript" src="../script/jquery-1.8.2.min.js"></script>
<script type="text/javascript">
    //光标所在id

    var idSelector = "";
    var seqNo = "";
    var arrSave = [];
    var num = 0;
    var mrlnum = 0;
    //初始化查询仓库下拉框
    apiready = function () {
        $("#skiddingCode").val(api.pageParam.skiddingCode)
        seqNo = api.pageParam.seq
        api.setFocus({
            inputId: 'containerTm',
            focus: true
        })
        search()
    }
    // 获取光标所在组件的id
    function getIdSelector() {
        var e = event || window.event;
        var el = e.srcElement || e.target;
        idSelector = el.id;
        $("#" + el.id).select();
    }
    //扫描
    function doScan() {
        var moduleScanner = api.require('moduleScanner');
        var params = {
            top: 150, //扫描窗口的上边距
            width: 250, //扫描窗口的宽
            height: 250, //扫描窗口的高
            useColor: false, //是否使用自己设定的颜色(startColor和endColor),
            //如果不使用设定的颜色就使用默认的那张网格图
            startColor: '#0F08C8FF', //渐变起始颜色
            endColor: '#7F08C8FF', //渐变结束颜色
            animTime: 2200, //扫描动画时间,单位为毫秒
        };
        moduleScanner.toScannerActivityForResult(params, function (ret, err) {
            if (idSelector == 'containerTm') {

                $("#containerTm").val(ret);
                save();

            }
        });
    }

    $("#containerTm").on('keypress', function (e) {
        var keycode = e.keyCode;
        var tm = $(this).val();
        if (keycode == '13') {
            if (tm != null && tm != "" && tm != undefined) {
                $("#containerTm").blur();

                var arr = tm.split(",");
                if (arr.length == 10) {
                    //查询箱码当前绑定数量
                    // doSearchBoxBindQty(tm);
                }
                else {
                    //查询容器当前绑定数量
                    save();
                }
            }
        }
    });

    function save() {

        var skiddingCode = $("#skiddingCode").val()
        var containerTm = $("#containerTm").val();
        if (containerTm == null || containerTm == "" || containerTm == undefined) {
            api.toast({
                msg: '请扫描容器条码！',
                duration: 3000
            });
            return;
        }
        var un = localStorage.getItem('un');
        var serverUrl = localStorage.getItem('serverUrl');
        var url = serverUrl + "/app/saveOnlineLot";
        var data = {
            values: { ////以表单方式提交参数（JSON对象）
                containerTm: containerTm,
                skiddingCode: skiddingCode,
                seqNo: seqNo,
                un: un
            }
        };
        api.ajax({
            url: url,
            method: 'post',
            dataType: 'json',
            timeout: 300,
            returnAll: false,
            data: data
        }, function (ret, err) {
            if (ret) {
                var data = JSON.stringify(ret);
                var myobj = eval("(" + data + ")");
                if (myobj.code == '0') {
                    api.toast({
                        msg: "绑定成功",
                        duration: 3000
                    });
                    var contentMrlToal = myobj.contentMrlToal == null ? 0 : myobj.contentMrlToal
                    var bindMrlToal = myobj.bindMrlToal == null ? 0 : myobj.bindMrlToal
                    var checkMrlCode = myobj.checkMrlCode == null ? "" : myobj.checkMrlCode
                    var messagetip = "容器条码：" + containerTm + "，总量：" + contentMrlToal + "，<br>"
                        + "物料编码：" + checkMrlCode + "，本次绑定数量：" + bindMrlToal
                    // $("#message").val(messagetip)
                    document.getElementById("message").innerHTML = messagetip

                    $("#containerTm").val("")
                    search()
                    $("#containerTm").focus();
                    idSelector = "containerTm";
                } else {
                    api.toast({
                        msg: myobj.message,
                        duration: 3000
                    });
                    var contentMrlToal = myobj.contentMrlToal == null ? 0 : myobj.contentMrlToal
                    var bindMrlToal = myobj.bindMrlToal == null ? 0 : myobj.bindMrlToal
                    var checkMrlCode = myobj.checkMrlCode == null ? "" : myobj.checkMrlCode
                    if (parseInt(bindMrlToal) > 0) {
                        var messagetip = "容器条码：" + containerTm + "，总量：" + contentMrlToal + "，<br>"
                            + "物料编码：" + checkMrlCode + "，本次绑定数量：" + bindMrlToal
                        document.getElementById("message").innerHTML = messagetip + "<br>" + myobj.message
                        search()
                    } else {
                        document.getElementById("message").innerHTML = myobj.message
                    }

                    $("#containerTm").val("")

                    $("#containerTm").focus();
                    idSelector = "containerTm";
                }
                if (myobj.GDflag == undefined || myobj.GDflag == null || myobj.GDflag == "") {

                } else {
                    api.confirm({
                        title: '是否进行归档操作？',
                        msg: "容器【" + containerTm + "】内数量为0，是否进行归档？归档后该容器已绑定物料不允许撤销！",
                        buttons: ['确定', '取消']
                    }, function (ret, err) {
                        var index = ret.buttonIndex;
                        if (index == 1) {
                            // 进行归档操作
                            saveGD(containerTm);

                        } else {

                            $("#containerTm").val("");

                            api.setFocus({
                                inputId: 'containerTm',
                                focus: true
                            })
                        }
                    });
                }
            }
        });
    }
    function search() {
        var arrSave = [];
        var skiddingCode = $("#skiddingCode").val()
        var un = localStorage.getItem('un');
        var serverUrl = localStorage.getItem('serverUrl');
        var url = serverUrl + "/app/getLineOnOffRecord";
        var data = {
            values: { ////以表单方式提交参数（JSON对象）
                skiddingCode: skiddingCode,
                seq: seqNo,
                un: un
            }
        };
        api.ajax({
            url: url,
            method: 'post',
            dataType: 'json',
            timeout: 300,
            returnAll: false,
            data: data
        }, function (ret, err) {
            if (ret) {
                var data = JSON.stringify(ret);
                var myobj = eval("(" + data + ")");
                if (myobj.code == '0') {
                    var list = eval(myobj.list);
                    if (list.length > 0) {

                        setTable(list)
                    } else {
                        api.toast({
                            msg: "请先维护治具与滑橇绑定关系！",
                            duration: 3000
                        });
                    }
                } else {
                    api.toast({
                        msg: myobj.message,
                        duration: 3000
                    });
                }
            }
        });
    }
    // 保存容器条码归档
    function saveGD(rqtm) {
        var array1 = [];

        array1.push(rqtm);

        var un = localStorage.getItem('un');
        var serverUrl = localStorage.getItem('serverUrl');
        var url = serverUrl + "/app/containerArchiveSave";
        var data = {
            values: { ////以表单方式提交参数（JSON对象）
                ctBarcodes: array1, //容器条码
                un: un
            }
        };
        api.ajax({
            url: url,
            method: 'post',
            dataType: 'json',
            timeout: 300,
            returnAll: false,
            data: data
        }, function (ret, err) {
            if (ret) {
                var data = JSON.stringify(ret);
                var myobj = eval("(" + data + ")");
                //成功
                if (myobj.code == '0') {
                    api.hideProgress();
                    api.toast({
                        msg: "归档成功",
                        duration: 3000
                    });

                    $("#containerTm").val("");

                    api.setFocus({
                        inputId: 'containerTm',
                        focus: true
                    })
                } else if (myobj.code == '1') {
                    api.hideProgress();
                    api.toast({
                        msg: myobj.message,
                        duration: 3000
                    });
                }
            }
        });


    }
    function finishOther() {
        api.confirm({
            title: '温馨提示',
            msg: '您确定要完成滑橇号' + api.pageParam.skiddingCode + '的上线绑定吗？提交后不可再绑定！',
            buttons: ['确定', '取消']
        }, function (ret, err) {
            if (ret.buttonIndex == 1) {
                savePlan()
            }
        });
    }
    function setTable(list) {
        var html = "";
        for (var i = 0; i < list.length; i++) {
            html += "<div class='white_background'>"
            html += '<ul class="aui-list aui-form-list">'
            html += '<li class="aui-list-item" style="background-color: #03a9f4;">'
            html += '<div class="aui-list-item-inner" >'
            html += '<input type="text" class="aui-input" style="width: 80%;font-size: 14px;" readonly id="skiddingCode" value="' + '层数：' + list[i].level + '&nbsp' + '治具：' + list[i].fixtureCode + '(容量：' + list[i].numTotal + ')' + '">'
            html += '</input>'
            html += '</div>'
            html += '</li>'
            html += '</ul>'
            html += '<div style="background-color:white; width: 100%;white-space: nowrap;overflow: auto;-webkit-overflow-scrolling:touch;">'
            html += '<table class="table" style="width:960px;" >'
            html += '<tbody style="display:block">'
            html += '<tr style="text-align: center;background-color: #DCE9F2;color: #467aa7;font-size: 15px;font-weight: bold;">'
            html += '<td algin="center" style="width:30px;"></td>'
            html += '<td algin="center" style="display:none">sysid</td>'
            html += '<td algin="center" style="width:130px;">规格</td>'
            html += '<td algin="center" style="width:200px;">物料名称</td>'
            html += '<td algin="center" style="width:130px;">容器条码</td>'
            html += '<td algin="center" style="width:130px;">物料编码</td>'
            html += '<td algin="center" style="width:70px;">数量</td>'
            html += '<td algin="center" style="width:100px;">批次号</td>'
            html += '<td algin="center" style="width:150px;">上线时间</td>'
            html += '</tr>'
            html += '</tbody>'
            html += '<tfoot style="display: block;overflow-y: scroll;">'
            if (list[i].onlineRecordList.length > 0) {
                var onlineRecordListNew = list[i].onlineRecordList
                var j
                for (j = 0; j < onlineRecordListNew.length; j++) {
                    html += '<tr style="text-align: center;color: black;font-size: 12px;background-color: #F5F5F5;">'
                    html += '<td algin="center" style="width:30px;"><input name="sub" type="checkbox" /></td>'
                    // html += '<td algin="center" style="width:50px;">' + (j + 1) + '</td>'
                    html += '<td algin="center" style="display:none" name="sysid">' + onlineRecordListNew[j].sysid + '</td>'
                    html += '<td algin="center" style="width:130px;" name="drawing">' + onlineRecordListNew[j].drawing + '</td>'
                    html += '<td algin="center" style="width:200px;" name="mrlName">' + onlineRecordListNew[j].mrlName + '</td>'
                    html += '<td algin="center" name="produBarcode" style="width:130px;">' + onlineRecordListNew[j].produBarcode + '</td>'
                    html += '<td algin="center" style="width:130px;" name="mrlCode">' + onlineRecordListNew[j].mrlCode + '</td>'
                    html += '<td algin="center" style="width:70px;">' + onlineRecordListNew[j].onlineQty + '</td>'
                    html += '<td algin="center" style="width:100px;" name="lotCode">' + onlineRecordListNew[j].lotCode + '</td>'
                    html += '<td algin="center" style="width:150px;">' + onlineRecordListNew[j].onlineDate + '</td>'
                    html += '</tr>'
                }
            } else {
                for (var j = 0; j < 1; j++) {
                    html += '<tr style="text-align: center;color: black;font-size: 12px;background-color: #F5F5F5;">'
                    html += '<td algin="center" style="width:30px;"><input name="sub" type="checkbox" /></td>'
                    // html += '<td algin="center" style="width:50px;">' + (j + 1) + '</td>'
                    html += '<td algin="center" style="display:none" name="sysid"></td>'
                    html += '<td algin="center" style="width:130px;" name="drawing">' + '</td>'
                    html += '<td algin="center" style="width:200px;" name="mrlName">' + '</td>'
                    html += '<td algin="center" name="produBarcode" style="width:130px;">' + '</td>'
                    html += '<td algin="center" style="width:130px;" name="mrlCode">' + '</td>'
                    html += '<td algin="center" style="width:70px;">' + '</td>'
                    html += '<td algin="center" style="width:100px;" name="lotCode">' + '</td>'
                    html += '<td algin="center" style="width:150px;">' + '</td>'
                    html += '</tr>'
                }
            }
            html += '</tfoot>'
            html += '</table>'
            html += '</div>'        
            html += '</div>'
            $api.html($api.byId("table1"), html);
        }
    };
    function chooseUnbind() {

        var containerTmCheck = "";
        var sysidCheck = "";
        var check = $("input[type='checkbox']:checked"); //在table中找input下类型为checkbox属性为选中状态的数据
        var count = 0;
        var flag = "";
        check.each(function () { //遍历
            var row = $(this).parent("td").parent("tr");
            containerTmCheck = row.find("[name='produBarcode']").html();
            sysidCheck = row.find("[name='sysid']").html();
            count++;
        });

        if (count != 1) {
            api.toast({
                msg: '请选择一条数据！',
                duration: 3000
            });
            return;
        }
        api.confirm({
            title: '温馨提示',
            msg: '您确定要解绑该条明细吗？',
            buttons: ['确定', '取消']
        }, function (ret, err) {
            if (ret.buttonIndex == 1) {
                var skiddingCode = $("#skiddingCode").val()
                var un = localStorage.getItem('un');
                var serverUrl = localStorage.getItem('serverUrl');
                var url = serverUrl + "/app/saveChooseUnbindLot";
                var data = {
                    values: { ////以表单方式提交参数（JSON对象）
                        skiddingCode: skiddingCode,
                        containerTm: containerTmCheck,
                        sysid: sysidCheck,
                        un: un
                    }
                };
                api.ajax({
                    url: url,
                    method: 'post',
                    dataType: 'json',
                    timeout: 300,
                    returnAll: false,
                    data: data
                }, function (ret, err) {
                    if (ret) {
                        var data = JSON.stringify(ret);
                        var myobj = eval("(" + data + ")");
                        if (myobj.code == '0') {
                            api.toast({
                                msg: "解绑成功",
                                duration: 3000
                            });
                            search()
                        } else {
                            api.toast({
                                msg: myobj.message,
                                duration: 3000
                            });
                        }
                    }
                });
            }
        });

    }
    function savePlan() {
        var skiddingCode = $("#skiddingCode").val()
        var un = localStorage.getItem('un');
        var serverUrl = localStorage.getItem('serverUrl');
        var url = serverUrl + "/app/saveSkiddingOnlinePlan";
        var data = {
            values: { ////以表单方式提交参数（JSON对象）
                seq: seqNo,
                skiddingCode: skiddingCode,
                ZSType: 'lot',
                un: un
            }
        };
        api.ajax({
            url: url,
            method: 'post',
            dataType: 'json',
            timeout: 300,
            returnAll: false,
            data: data
        }, function (ret, err) {
            if (ret) {
                var data = JSON.stringify(ret);
                var myobj = eval("(" + data + ")");
                if (myobj.code == '0') {
                    api.toast({
                        msg: "保存成功",
                        duration: 3000
                    });
                    api.closeWin()
                    api.execScript({
                        name: api.pageParam.name,
                        script: 'search()'
                    });
                } else {
                    api.toast({
                        msg: myobj.message,
                        duration: 3000
                    });
                }
            }
        });
    }

    //返回按钮
    function closeWin() {
        api.closeWin({});
    }
</script>

</html>